﻿using System;
using System.ComponentModel;
using NerdWare.Crm2011VsTools.Core;
using NerdWare.Crm2011VsTools.Globals;

namespace NerdWare.Crm2011VsTools.DialogPages
{
    public class CrmServerConnection
    {
        private string _server = "servername";

        [Category(Category.CrmServer)]
        [DisplayName("Crm Server Name")]
        [Description("The host name on which CRM is running.")]
        public string Server
        {
            get { return _server; }
            set
            {
                if (Type == CrmType.Crm2011OnPremise)
                {
                    _server = value;
                }
            }
        }


        private int _port = 5555;

        [Category(Category.CrmServer)]
        [DisplayName("Crm Server Port")]
        [Description("The port on which CRM is running. Usually it is 5555 in a default OnPremise installation.")]
        public int Port
        {
            get { return _port; }
            set
            {
                if (Type == CrmType.Crm2011OnPremise)
                {
                    _port = value;
                }
            }
        }


        private string _organization = "BasicOrg";

        [Category(Category.CrmServer)]
        [DisplayName("Organization")]
        [Description("The Crm Organization name.")]
        public string Organization
        {
            get { return _organization; }
            set
            {
                _organization = value;
                generateServerName();
            }
        }


        private CrmType _type = CrmType.Crm2011OnPremise;

        [Category(Category.CrmServer)]
        [DisplayName("Crm Type")]
        [Description("Type of CRM Installation")]
        public CrmType Type
        {
            get { return _type; }
            set
            {
                _type = value;
                generateServerName();
            }
        }

        private bool _useCustomFolder = false;

        [Category(Category.Fiddler)]
        [DisplayName("Use Custom Folder")]
        [Description("If true, you will use the Server specific Settings.")]
        public bool UseCustomFolder
        {
            get { return this._useCustomFolder; }
            set
            { this._useCustomFolder = value; }
        }

        private string _scriptFolder = @"c:\";

        [Category(Category.Fiddler)]
        [DisplayName("ScriptFolder")]
        [Description("The Folder with the replacement files located.")]
        public string ScriptFolder
        {
            get { return this._scriptFolder; }
            set { this._scriptFolder = value; }
        }

        public string DisplayName
        {
            get
            {
                return Type == CrmType.Crm2011OnPremise ?
                String.Format("{0}/{1} [{2}]", Server, Organization, Type)
                : String.Format("{0} [{1}]", Server, Type);
            }
        }

        public override string ToString()
        {
            return DisplayName;
        }

        private void generateServerName()
        {
            switch (Type)
            {
                case CrmType.Crm2011OnlineNorthAmericaLiveId:
                case CrmType.Crm2011OnlineNorthAmericaOffice365:
                    _server = string.Format("{0}.crm.dynamics.com", Organization);
                    _port = 443;
                    break;
                case CrmType.Crm2011OnlineEMEALiveId:
                case CrmType.Crm2011OnlineEMEAOffice365:
                    _server = string.Format("{0}.crm4.dynamics.com", Organization);
                    _port = 443;
                    break;
                case CrmType.Crm2011OnlineAPACLiveId:
                case CrmType.Crm2011OnlineAPACOffice365:
                    _server = string.Format("{0}.crm5.dynamics.com", Organization);
                    _port = 443;
                    break;
            }
        }
    }
}
